package com.kly.cashmall.utils.data;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

/**
 * @author zxp
 * @since 26/10/21
 */
public class GsonUtils {
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public GsonUtils() {
    }

    public static String toJson(Object target) {
        return toJson(target, (String)null);
    }

    public static String toJson(Object target, String datePattern) {
        if (target == null) {
            return "";
        } else {
            String result = null;
            if (TextUtils.isEmpty(datePattern)) {
                result = (new Gson()).toJson(target);
            } else {
                GsonBuilder builder = new GsonBuilder();
                builder.setDateFormat(datePattern);
                result = builder.create().toJson(target);
            }

            return result;
        }
    }

    public static <T> T fromJson(String json, TypeToken<T> token, String datePattern) {
        if (TextUtils.isEmpty(json)) {
            return null;
        } else {
            GsonBuilder builder = new GsonBuilder();
            if (TextUtils.isEmpty(datePattern)) {
                datePattern = "yyyy-MM-dd HH:mm:ss";
            }

            builder.setDateFormat(datePattern);
            Gson gson = builder.create();

            try {
                return gson.fromJson(json, token.getType());
            } catch (Exception var6) {
                var6.printStackTrace();
                return null;
            }
        }
    }

    public static <T> T fromJson(String json, Class<T> clazz, String datePattern) {
        if (TextUtils.isEmpty(json)) {
            return null;
        } else {
            GsonBuilder builder = new GsonBuilder();
            if (TextUtils.isEmpty(datePattern)) {
                datePattern = "yyyy-MM-dd HH:mm:ss";
            }

            builder.setDateFormat(datePattern);
            Gson gson = builder.create();

            try {
                return gson.fromJson(json, clazz);
            } catch (Exception var6) {
                var6.printStackTrace();
                return null;
            }
        }
    }

    public static <T> T fromJson(String json, Type type, String datePattern) {
        if (TextUtils.isEmpty(json)) {
            return null;
        } else {
            GsonBuilder builder = new GsonBuilder();
            if (TextUtils.isEmpty(datePattern)) {
                datePattern = "yyyy-MM-dd HH:mm:ss";
            }

            builder.setDateFormat(datePattern);
            Gson gson = builder.create();

            try {
                return gson.fromJson(json, type);
            } catch (Exception var6) {
                var6.printStackTrace();
                return null;
            }
        }
    }

    public static <T> T fromJson(String json, Class<T> clazz) {
        return (T) fromJson(json, (Class)clazz, (String)null);
    }

    public static <T> T fromJson(String json, Type type) {
        return fromJson(json, (Type)type, (String)null);
    }

    public static <T> T fromJson(String json, TypeToken<T> token) {
        return (T) fromJson(json, (TypeToken)token, (String)null);
    }

    public static Gson getGson() {
        GsonBuilder builder = new GsonBuilder();
        builder.setDateFormat("yyyy-MM-dd HH:mm:ss");
        return builder.create();
    }
}
